package de.koller.worlders.engine.util;

import de.koller.oceanFramework.tree.Mother;
import de.koller.worlders.WSea;
import de.koller.worlders.engine.entity.AABBObject;
import de.koller.worlders.engine.entity.IRenderable;

public class Animation extends WSea {
	
	private boolean mx = false;
	private boolean my = false;
	private final IRenderable frames[];
	@Mother private AABBObject aaabbObject;

	private int tick = 500;
	private int currentTick = 0;
	private long nextTick = 0;
	private int[] sequence;
	
	public Animation( IRenderable... frames ) {
		this.frames = frames;
	}

	@Override
	protected void update( int elapsedTime ) {
		super.update( elapsedTime );
		
		if( nextTick < wroot.getTime() ) {
			currentTick++;
			nextTick = wroot.getTime() + tick;
		}
		
		if( sequence == null ) 
			return;
		
		currentTick %= sequence.length;
		this.use( sequence[currentTick] ); 
	}
	
	public void use( int frame, boolean mx, boolean my ) {
		this.mx = mx;
		this.my = my;
		aaabbObject.setMirrorX( mx );
		aaabbObject.setMirrorY( my );
		aaabbObject.setVisual( frames[frame] );
	}
	
	public void use( int frame ) {
		this.use( frame, mx, my );
	}

	public void animateSequenze( int... sequence ) {
		if( sequence != this.sequence )
			currentTick = 0;
		this.sequence = sequence;
	}
	
	public void setMx( boolean mx ) {
		this.mx = mx;
	}

	public void setMy( boolean my ) {
		this.my = my;
	}

	public void setTick( int tick ) {
		this.tick = tick;
	}
	
}
